﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastSafeDeployM8.Classes;

namespace FastSafeDeployM8
{
    public partial class MainForm : Form
    {
        
        List<string> listQueue = new List<string>();

        public MainForm()
        {
            InitializeComponent();
            FileExplorerUtils.loadTreeDrives(treeLeft);
            FileExplorerUtils.loadTreeDrives(treeRight);

            

            //TEST force :
            tbTreeLeftPath.Text = @"F:\MyProjects\FastSafeDeployM8\FastSafeDeployM8\TEST\Packages\2015.02.22\cm";
            tbTreeRightPath.Text = @"F:\MyProjects\FastSafeDeployM8\FastSafeDeployM8\TEST\LIVE_SERVER\cm";

            FileExplorerUtils.reloadTreeFromPath(treeLeft, tbTreeLeftPath.Text);
            FileExplorerUtils.reloadTreeFromPath(treeRight, tbTreeRightPath.Text);
        }

        private void tree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            TreeView treeView = (TreeView)(sender);
            TreeNode currentTreeNode = e.Node;

            //MessageBox.Show(currentTreeNode.Index.ToString());

            if (currentTreeNode.Level > 0)
            {
                currentTreeNode.Nodes.Clear();

                FileExplorerUtils.expandTreeNode(currentTreeNode);
            }
        }

        private void bTreeLeftPathBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog_Paths.ShowDialog();
            tbTreeLeftPath.Text = folderBrowserDialog_Paths.SelectedPath;

            FileExplorerUtils.reloadTreeFromPath(treeLeft, tbTreeLeftPath.Text);
        }

        private void bTreeRightPathBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog_Paths.ShowDialog();
            tbTreeRightPath.Text = folderBrowserDialog_Paths.SelectedPath;
            
            FileExplorerUtils.reloadTreeFromPath(treeRight, tbTreeRightPath.Text);
        }

        private void treeLeft_AfterSelect(object sender, TreeViewEventArgs e)
        {
            tbTreeLeftPath.Text = FileExplorerUtils.getNodePath(e.Node);
        }

        private void treeRight_AfterSelect(object sender, TreeViewEventArgs e)
        {
            tbTreeRightPath.Text = FileExplorerUtils.getNodePath(e.Node);
        }

        private void tbTreeLeftPath_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                FileExplorerUtils.reloadTreeFromPath(treeLeft, tbTreeLeftPath.Text);
            }
        }

        private void tbTreeRightPath_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                FileExplorerUtils.reloadTreeFromPath(treeRight, tbTreeRightPath.Text);
            }
        }

        private void bSwap_Click(object sender, EventArgs e)
        {
            string tmpPath = tbTreeLeftPath.Text;
            tbTreeLeftPath.Text = tbTreeRightPath.Text;
            tbTreeRightPath.Text = tmpPath;
             
            FileExplorerUtils.reloadTreeFromPath(treeLeft, tbTreeLeftPath.Text);
            FileExplorerUtils.reloadTreeFromPath(treeRight, tbTreeRightPath.Text);
        }

        private void bRightToLeft_Click(object sender, EventArgs e)
        {
            tbTreeLeftPath.Text = tbTreeRightPath.Text;
            FileExplorerUtils.reloadTreeFromPath(treeLeft, tbTreeLeftPath.Text);
        }

        private void bLeftToRight_Click(object sender, EventArgs e)
        {
            tbTreeRightPath.Text = tbTreeLeftPath.Text;
            FileExplorerUtils.reloadTreeFromPath(treeRight, tbTreeRightPath.Text);
        }

        private void bCopy_Click(object sender, EventArgs e)
        {
            lbQueue.Items.Add("copy " + FileExplorerUtils.SEPARATOR[0] + tbTreeLeftPath.Text + FileExplorerUtils.SEPARATOR[0] + tbTreeRightPath.Text);
        }

        private void bExecuteQueue_Click(object sender, EventArgs e)
        {
            FileExplorerUtils.executeQueueOperations(lbQueue);
        }

        private void bRemoveQueue_Click(object sender, EventArgs e)
        {
            if (lbQueue.Items.Count >= 1)
            {
                if (lbQueue.SelectedItem != null)
                {
                    lbQueue.Items.Remove(lbQueue.SelectedItem);
                }
            }
        }

        private void bMoveQueueUp_Click(object sender, EventArgs e)
        {
            if (lbQueue.Items.Count >= 2)
            {
                if (lbQueue.SelectedItem != null)
                {
                    if (lbQueue.SelectedIndex > 0)
                    {
                        string tempItem = (string)lbQueue.Items[lbQueue.SelectedIndex - 1];
                        lbQueue.Items[lbQueue.SelectedIndex - 1] = lbQueue.Items[lbQueue.SelectedIndex];
                        lbQueue.Items[lbQueue.SelectedIndex] = tempItem;
                        lbQueue.SelectedIndex = lbQueue.SelectedIndex - 1;
                    }
                }
            }
        }

        private void bMoveQueueDown_Click(object sender, EventArgs e)
        {
            if (lbQueue.Items.Count >= 2)
            {
                if (lbQueue.SelectedItem != null)
                {
                    if (lbQueue.SelectedIndex < lbQueue.Items.Count-1)
                    {
                        string tempItem = (string)lbQueue.Items[lbQueue.SelectedIndex + 1];
                        lbQueue.Items[lbQueue.SelectedIndex + 1] = lbQueue.Items[lbQueue.SelectedIndex];
                        lbQueue.Items[lbQueue.SelectedIndex] = tempItem;
                        lbQueue.SelectedIndex = lbQueue.SelectedIndex + 1;
                    }
                }
            }
        }

        private void bBackup_Click(object sender, EventArgs e)
        {
            tbBackupPath.Text = tbTreeLeftPath.Text;
        }
    }
}
